package com.jingding.procurement.controller;

import com.alibaba.fastjson.JSON;
import com.jingding.procurement.enumeration.EntityEnum;
import com.jingding.procurement.pojo.entity.*;
import com.jingding.procurement.service.*;
import com.jingding.procurement.util.TimeFormat;
import com.jingding.procurement.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("procureMarket")
/*采购销售接口*/
public class ProcureMarketController {
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ProcurementContractService procurementContractService;
    @Autowired
    private PurchaseOrdersService purchaseOrdersService;
    @Autowired
    private PurchasePoundsService purchasePoundsService;
    @Autowired
    private SellOutService sellOutService;
    @Autowired
    private TonnageService tonnageService;

    /*推送采购合同接口*/
    @PostMapping("pushProcurementContract")
    public JsonResult pushProcurementContract(@RequestBody Entity entity) throws IOException, ClassNotFoundException {
        if (entity.getPort().equals(EntityEnum.PCE.getCode())) {
           return procurementContractService.pushProcurementContract(entity, TimeFormat.secondTime());
        } else {
            log.debug("[采购合同]推送失败,时间: {}", TimeFormat.secondTime());
            return JsonResult.fail(400, "[采购合同]推送失败,时间:" + TimeFormat.secondTime());
        }
    }

    /*推送供方检验单接口*/
    @PostMapping("pushSupplierInspectionSheet")
    public JsonResult pushSupplierInspectionSheet(@RequestBody Entity entity) throws IOException, ClassNotFoundException {
        if (entity.getPort().equals(EntityEnum.SISE.getCode())) {
            return supplierService.pushSupplierInspectionSheet(entity, TimeFormat.secondTime());
        }

        log.debug("[供方检验单]推送失败,时间: {}", TimeFormat.secondTime());
        return JsonResult.fail(400, "[供方检验单]推送失败,请输入正确的PORT:");
    }

    /*推送采购订单接口*/
    @PostMapping("pushPurchaseOrders")
    public JsonResult pushPurchaseOrders(@RequestBody Entity entity) throws IOException, ClassNotFoundException {
        if (entity.getPort().equals(EntityEnum.POE.getCode())) {
            return purchaseOrdersService.pushPurchaseOrders(entity, TimeFormat.secondTime());
        } else {
            log.debug("[采购订单]推送失败,时间: {}", TimeFormat.secondTime());
            return JsonResult.fail(400, "[采购订单]推送失败,时间:" + TimeFormat.secondTime());
        }
    }


    /*推送采购磅单接口*/
    @PostMapping("pushPurchasePounds")
    public JsonResult pushPurchasePounds(@RequestBody Entity entity) throws IOException, ClassNotFoundException {
        if (entity.getPort().equals(EntityEnum.PPE.getCode())) {
            return purchasePoundsService.pushPurchasePounds(entity, TimeFormat.secondTime());
        } else {
            log.debug("[采购磅单]推送失败,时间: {}", TimeFormat.secondTime());
            return JsonResult.fail(400, "[采购磅单]推送失败,时间:" + TimeFormat.secondTime());

        }
    }

    /*推送销售出库接口*/
    @PostMapping("pushSellOut")
    public JsonResult pushSellOut(@RequestBody Entity entity) throws IOException, ClassNotFoundException {
        if (entity.getPort().equals(EntityEnum.SOE.getCode())) {
            return sellOutService.pushSellOut(entity, TimeFormat.secondTime());
        } else {
            log.debug("[销售出库]推送失败,时间: {}", TimeFormat.secondTime());
            return JsonResult.fail(400, "[销售出库]推送失败,时间:" + TimeFormat.secondTime());

        }
    }

    /*扣吨计算单*/
    @PostMapping("pushTonnage")
    public JsonResult pushTonnage(@RequestBody Entity entity) throws IOException, ClassNotFoundException {
        if (entity.getPort().equals(EntityEnum.TE.getCode())) {
            return tonnageService.pushTonnage(entity, TimeFormat.secondTime());
        } else {
            log.debug("[扣吨计算单]推送失败,时间: {}", TimeFormat.secondTime());
            return JsonResult.fail(400, "[扣吨计算单]推送失败,时间:" + TimeFormat.secondTime());
        }
    }

}
